package com.ja.openchat.ui.me;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.IntRange;
import androidx.annotation.Nullable;

import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.databinding.FragmentExpressionStoreDetailBinding;
import com.ja.openchat.viewmodels.ExpressionStoreDetailViewModel;

import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.utils.KLog;

//表情商店详情
public class ExpressionStoreDetailFragment extends BaseFragment<FragmentExpressionStoreDetailBinding, ExpressionStoreDetailViewModel> {


    public static ExpressionStoreDetailFragment newInstance(@IntRange(from = 0, to = 2) int type) {
        ExpressionStoreDetailFragment fragment = new ExpressionStoreDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_expression_store_detail;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public void initView() {
        binding.refreshLayout.setEnableLoadMore(false);
        binding.titleBar.setOnIconRightListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //表情管理
                Bundle bundle = new Bundle();
                bundle.putInt("type", 0);
                startContainerActivity(ExpressionStoreDetailFragment.class.getCanonicalName(), bundle);
            }
        });
    }

    @Override
    public void initData() {
        int type = 0;
        if (getArguments() != null) {
            type = getArguments().getInt("type");
        }
        viewModel.setType(type);
        binding.refreshLayout.autoRefresh();
        Observable.timer(1, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(aLong -> {
            if (binding.refreshLayout != null)
                binding.refreshLayout.finishRefresh();
        });
    }

    @Override
    public void initViewObservable() {
        //监听下拉刷新完成
        viewModel.uc.finishRefreshing.observe(this, o -> {
            //结束刷新
            binding.refreshLayout.finishRefresh();
        });
        //监听上拉加载完成
        viewModel.uc.finishLoadmore.observe(this, o -> {
            //结束刷新
            binding.refreshLayout.finishLoadMore();
        });
        //监听是否启用加载更多
        viewModel.uc.enableFinishLoadmore.observe(this, aBoolean -> {
            KLog.d("enableFinishLoadmore==" + aBoolean);
            binding.refreshLayout.setEnableLoadMore(aBoolean);
        });
    }
}
